import 'dart:io' show Platform;
import 'dart:ui';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:bitsdojo_window/bitsdojo_window.dart';
import 'app_navigator.dart';
import 'application.dart';
import 'config.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
      overlays: [SystemUiOverlay.bottom, SystemUiOverlay.top]);
  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
  await Application.getInstance();

  runApp(const ShirneApp());
  if (!kIsWeb && (Platform.isWindows || Platform.isMacOS || Platform.isLinux)) {
    doWhenWindowReady(() {
      const initialSize = Size(480, 854);
      appWindow.minSize = initialSize;
      appWindow.size = initialSize;
      appWindow.alignment = Alignment.center;
      appWindow.title = '临风小筑';
      appWindow.show();
    });
  }
}

class ShirneApp extends StatefulWidget {
  const ShirneApp({Key? key}) : super(key: key);

  @override
  State<ShirneApp> createState() => _ShirneAppState();
}

class _ShirneAppState extends State<ShirneApp> {
  MaterialColor mColor = Colors.blue;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Config.init(context);
    return MaterialApp(
      title: '',
      onGenerateTitle: (BuildContext context) {
        if (!kIsWeb &&
            (Platform.isWindows || Platform.isMacOS || Platform.isLinux)) {
          appWindow.title = '临风小筑';
        }
        return '临风小筑';
      },
      theme: ThemeData.light().copyWith(
        scaffoldBackgroundColor: const Color.fromRGBO(240, 240, 240, 1),
        visualDensity: VisualDensity.standard,
      ),
      darkTheme: ThemeData.dark().copyWith(
        textTheme: Typography.dense2018,
        visualDensity: VisualDensity.standard,
      ),
      highContrastTheme: ThemeData.light().copyWith(
        textTheme: Typography.dense2018,
        visualDensity: VisualDensity.standard,
      ),
      highContrastDarkTheme: ThemeData.dark().copyWith(
        textTheme: Typography.dense2018,
        visualDensity: VisualDensity.standard,
      ),
      themeMode: ThemeMode.system,
      initialRoute: '/',
      routes: app.routes,
      builder: (context, widget) => Material(child: widget),
      scrollBehavior: MyCustomScrollBehavior(),
      navigatorObservers: [AppNavigatorObserver()],
    );
  }
}

class MyCustomScrollBehavior extends MaterialScrollBehavior {
  // Override behavior scrollbar
  @override
  Widget buildScrollbar(
      BuildContext context, Widget child, ScrollableDetails details) {
    return child;
  }

  // Override behavior methods and getters like dragDevices
  @override
  Set<PointerDeviceKind> get dragDevices => {
        PointerDeviceKind.touch,
        PointerDeviceKind.mouse,
        PointerDeviceKind.stylus,
        PointerDeviceKind.invertedStylus,
      };
}
